//
//  LoginPage.swift
//  Casecord
//
//  Created by baozi on 2024/1/3.
//

import SwiftUI

struct LoginPage: View {
    @StateObject var router: Router = Router()
    @State var protoSel: Bool = false
    var body: some View {
        NavigationStack(path: $router.path) {
            ZStack {
                Theme.color.primaryBackgroundColor.ignoresSafeArea()
                VStack(alignment: .center, spacing: 20) {
                    Text(" Here, \n Create a account there")
                        .foregroundColor(.white)
                        .bold()
                        .font(.title)
                    
                    Button(action: {
                        loginAction()
                    }, label: {
                        HStack(spacing: 15) {
                            Image(systemName: "phone")
                            Text("Login with phone")
                        }
                        .foregroundColor(Theme.color.primaryBackgroundColor)
                        .padding()
                        .withCapsuleBg(colors: [Color.white], shadow: .gray, radius: 5)
                        
                    })
                    HStack {
                        Button(action: {
                            self.protoSel.toggle()
                        }, label: {
                            if self.protoSel {
                                Image("pro_sel")
                            } else {
                                Image("pro_unsel")
                            }
                        })
                        Text("Privacy")
                            .underline()
                            .foregroundColor(Theme.color.title)
                            .onTapGesture {
                                privacyAction()
                            }
                        Text("&")
                            .foregroundColor(Theme.color.title)
                        Text("UserItem")
                            .foregroundColor(Theme.color.title)
                            .underline()
                            .onTapGesture {
                                userItemAction()
                            }
                    }
                }
            }
            .withAppRouter()
        }
        .environmentObject(router)
    }
    
    func loginAction() {
        if !protoSel {
            ToastManager.show(text: "Please agree to the Privacy Data Agreement and User Use Agreement first")
        } else  {
            router.to(name: .phoneLogin)
        }
        
    }
    
    func privacyAction() {
        UserService.shared.loginStep = .login
    }
    
    func userItemAction() {
        
    }
    
}

#Preview {
    LoginPage()
}
